package edu.hawaii.duedates;

import java.util.Calendar;

/**
 * A borrowed item.
 * 
 * @author Aric
 *
 */
public class BorrowedItem {
  private final String itemName;
  private final Calendar dueDate;
  
  /**
   * Contains a borrowed item name and it's due date.
   * 
   * @param dueDate
   * @param itemName
   */
  public BorrowedItem(Calendar dueDate, String itemName) {
    this.itemName = itemName;
    this.dueDate = dueDate;
  }

  /**
   * Gets the name of the borrowed item.
   * 
   * @return String
   */
  public String getItemName() {
    return this.itemName;
  }

  /**
   * Gets the due date of the borrowed item.
   * @return
   */
  public Calendar getDueDate() {
    return this.dueDate;
  }
  
  /**
   * Returns true when comparing a object to this object if they are the same
   * type and contain the same due date and item name.
   * 
   * @param o
   * @return True or false.
   */
  @Override
  public boolean equals(Object o) {
    if (o == this) {
      return true;
    }
    else if (!(o instanceof BorrowedItem)) {
      return false;
    }
    else {
      BorrowedItem b = (BorrowedItem) o;
      return (this.dueDate.equals(b.getDueDate()))
          && (this.itemName.equals(b.getItemName()));
    }
  }
  
  /**
   * Returns a hash code based on due date and item name.
   * 
   * @return Hash code.
   */
  @Override
  public int hashCode() {
    int result = 7;
    result += 31 * this.dueDate.hashCode();
    result += 31 * this.itemName.hashCode();
    return result;
  }
  
  /**
   * Returns string as: "mm-dd-yyyy | itemName".
   * 
   * @return String representation of this object.
   */
  @Override
  public String toString() {
    String result = String.format("%1$tm-%1$td-%1$tY", this.dueDate);
    result += " | " + itemName;
    return result;
  }
}
